/*
 * @lc app=leetcode.cn id=63 lang=javascript
 *
 * [63] 不同路径 II
 */

// @lc code=start
/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function (obstacleGrid) {
  let grid = obstacleGrid;
  for (let i = 0; i < grid.length; i++) {
    for (let j = 0; j < grid[i].length; j++) {
      if (obstacleGrid[i][j] === 1) {
        grid[i][j] = 0
      } else if (i === 0 && j === 0) {
        grid[i][j] = 1;
      } else if (i === 0) {
        grid[i][j] = grid[i][j - 1]
      } else if (j === 0) {
        grid[i][j] = grid[i - 1][j]
      } else {
        grid[i][j] = grid[i - 1][j] + grid[i][j - 1];
      }
    }
  }
  return grid[grid.length - 1][grid[0].length - 1];
};
// @lc code=end
